/**
 * 发票预核销单
 */
/**
 * 表单自定义控制器
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http) {
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增发票预核销单
			{
				//购买方公司名称
				var vname = $scope.valueMap['vname'];
				if (vname == null || vname == ''){
					return layer.msg("购买方公司名称未选择,不允许新增!");
				}
				//供应方公司名称
				var partnerName = $scope.valueMap['partnerName'];
				if (partnerName == null || partnerName == ''){
					return layer.msg("供应方公司名称未选择,不允许新增!");
				}

				//回填数据的代码
				var listUkey = [];
				for(var i=0;i<$scope.subGrid.sonlist.length;i++){
					var data = $scope.subGrid.sonlist[i];
					listUkey.push(data.itemId);
				}
				/*
				$scope.showModalCheckBox('TCG_PurchaseContractByManage_InvoiceOffset', 'itemId', listUkey,
					[{code: 'CI.Vname', value:vname},{code: 'PB.PartnerName', value:partnerName}]);

				 */

				$scope.showModalCheckBox('TCG_PurchaseContractByManage_InvoiceOffset', 'itemId', listUkey,
					[{code: 'vname', value:vname},{code: 'partnerName', value:partnerName}]);

				break;
			}
			case 'event-to-delete': // 删除选中数据
			{
				$scope.subGrid.deleteBatch();
				var offsetTotalAmount = 0 ;
				if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
					for(var i=0;i<$scope.subGrid.sonlist.length;i++){
						if($scope.subGrid.sonlist[i].offsetAmount){
							offsetTotalAmount = offsetTotalAmount+parseFloat($scope.subGrid.sonlist[i].offsetAmount);
						}
					}
				}
				$scope.valueMap['offsetTotalAmount'] = offsetTotalAmount.toFixed(2);
				break;
			}


		};
	}

}


app.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){

	/*
	 * 数字框变动
	 */
	$scope.subGrid.numberChange = function(row, field) {
		var index = row.index;
		if (field == 'offsetNum') {//监听发票本次核销数量
			if($scope.subGrid.sonlist[index]['offsetNum']) {
				var offsetNum = parseInt($scope.subGrid.sonlist[index]['offsetNum']);//发票本次核销数量
				var subPurchaseNum=parseInt($scope.subGrid.sonlist[index]['subPurchaseNum']);//子件采购数量
				var subWriteOffNum=parseInt($scope.subGrid.sonlist[index]['subWriteOffNum']);//子件已核销数量

				if (Number($scope.subGrid.sonlist[index]['offsetNum'])> Number(subPurchaseNum - subWriteOffNum)){
					layer.msg("发票本次核销数量数量不允许大于\"子件采购数量\"-\"子件已核销数量\"!");
					$scope.subGrid.sonlist[index]['offsetNum'] = 0;
				}else{
					//本次核销金额 = 发票本次核销数量*子件采购单价
					var subPurchasePrice=parseFloat($scope.subGrid.sonlist[index]['subPurchasePrice']);
					var offsetAmount = (offsetNum * subPurchasePrice).toFixed(2);
					$scope.subGrid.sonlist[index]['offsetAmount'] = offsetAmount;
				}

				var offsetTotalAmount = 0 ;
				if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
					for(var i=0;i<$scope.subGrid.sonlist.length;i++){
						if($scope.subGrid.sonlist[i].offsetAmount){
							offsetTotalAmount = offsetTotalAmount+parseFloat($scope.subGrid.sonlist[i].offsetAmount);
						}
					}
				}
				$scope.valueMap['offsetTotalAmount'] = offsetTotalAmount.toFixed(2);

			}


		}
	}
}



/*
* 子列表控制器
*/
app.controller('subGridCtrl', ['$scope', '$http', function SubGridController($scope, $http){
	//多选框回调
	$scope.checkbox.afterSelectDatas = function(rowsSelected) {
		for (var i = 0; i < rowsSelected.length; i++) {
			var row = rowsSelected[i];
			var isAdd = true; // 根据现有存在，是否加新行
			for (var j = 0; j < $scope.subGrid.sonlist.length; j++) {
				if ($scope.subGrid.sonlist[j].purchaseContractItemId == row.itemId) {
					isAdd = false;
				}
			}
			if (isAdd) {
				$scope.subGrid.sonlist.push({
					id:'',
					purchaseContractItemId: row.itemId,
					purchaseContractBid: row.bid,
					appointOrderBid: row.appointOrderBid,
					vgsbm: row.vgsbm,
					vgsjc: row.vgsjc,
					vnameByItem: row.vname,
					partnerNameByItem: row.partnerName,
					productCode: row.productCode,
					productName: row.productName,
					codeMerge: row.codeMerge,
					subCode: row.subCode,
					subName: row.subName,
					invoiceModelNo: row.invoiceModelNo,
					invoiceName: row.invoiceName,
					subCodeMerge: row.subCodeMerge,
					subBomNumber: row.subBomNumber,
					subPurchaseNum: row.subPurchaseNum,
					subPurchasePrice: row.subPurchasePrice,
					subWriteOffNum: row.subWriteOffNum,
					offsetNum:0,
					offsetAmount:0
				});
			}
		}

		var offsetTotalAmount = 0 ;
		if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
			for(var i=0;i<$scope.subGrid.sonlist.length;i++){
				if($scope.subGrid.sonlist[i].offsetAmount){
					offsetTotalAmount = offsetTotalAmount+parseFloat($scope.subGrid.sonlist[i].offsetAmount);
				}
			}
		}
		$scope.valueMap['offsetTotalAmount'] = offsetTotalAmount.toFixed(2);

	}
}]);
